/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.dialog;


import cn.ac.nercis.pes.community.support.controls.JFXDialog;
import cn.ac.nercis.pes.community.support.controls.JFXSpinner;
import cn.ac.nercis.pes.community.utils.GuiStore;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;


/**
 * 进度提示弹出框
 * @author zpy
 * @version v1.0.0
 */
public class SpinnerDialog {
    private final JFXDialog dialog;
    private final Label tipLabel;
    @Getter
    private final JFXSpinner spinner;
    @Getter
    private String processTip;
    @Getter
    @Setter
    private StackPane dialogContainer;
    @Getter
    private boolean isShow;

    public SpinnerDialog() {
        dialog = new JFXDialog();
        spinner = new JFXSpinner();
        spinner.getStyleClass().addAll("materialDesign-blue", "process-spinner");
        spinner.setStartingAngle(-40);
        tipLabel = new Label();
        tipLabel.getStyleClass().add("process-tip");
        isShow = false;
    }

    private SpinnerDialog(Builder builder){
        this();
        processTip = builder.processTip;
        dialogContainer = builder.dialogContainer;
    }

    public void show(){
        show(true);
    }

    public void show(boolean init){
        if(init){
            this.render();
        }
        this.dialog.show();
        this.isShow = true;
    }

    public void setProcessTip(String processTip){
        if(StringUtils.isNotEmpty(processTip)){
            this.tipLabel.setText(processTip);
        }
    }

    public void close(){
        this.dialog.close();
        this.isShow = false;
    }

    private void render(){
        this.dialog.setDialogContainer(dialogContainer);
        this.dialog.setOverlayClose(false);
        VBox container = new VBox();
        container.getStyleClass().add("process-container");
        container.setAlignment(Pos.CENTER);
        this.tipLabel.setText(this.processTip);
        container.getChildren().addAll(spinner,tipLabel);
        this.dialog.setContent(container);
    }

    public static Builder builder(){
        return new Builder();
    }

    public static final class Builder{
        private String processTip;
        private StackPane dialogContainer;

        private Builder(){
            dialogContainer = GuiStore.getRoot();
        }

        public Builder withProcessTip(String processTip){
            this.processTip = processTip;
            return this;
        }

        public Builder withContainer(StackPane dialogContainer){
            this.dialogContainer = dialogContainer;
            return this;
        }

        public SpinnerDialog build(){
            return new SpinnerDialog(this);
        }
    }
}